/**
 * Copyright &copy; 2013-2015 山东易科德软件有限公司   All rights reserved.
 */
package cn.net.ecode.modules.sys.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.service.CrudService;
import cn.net.ecode.modules.sys.dao.EmployeeDao;
import cn.net.ecode.modules.sys.entity.Employee;
import cn.net.ecode.modules.sys.entity.User;
import cn.net.ecode.modules.sys.utils.DataScopeUtils;

import com.google.common.collect.Lists;

/**
 * 员工管理Service
 * @author jiaoanjian、ThinkGem
 * @version 2015-4-24
 */
@Service
@Transactional(readOnly = true)
public class EmployeeService extends CrudService<EmployeeDao, Employee> {

	@Autowired
	private UserService userService;
	
	@Autowired
	private PostService postService;

	/**
	 * 根据编号获取员工
	 */
	public Employee get(String empCode) {
		return super.get(empCode);
	}

	/**
	 * 查询员工数据
	 */
	public List<Employee> findList(Employee employee) {
		// 生成数据权限过滤条件
		DataScopeUtils.genSql(employee, "dsf", "company_m,office_m", "c3,o2");
		return super.findList(employee);
	}

	/**
	 * 分页查询员工数据
	 */
	public Page<Employee> findPage(Page<Employee> page, Employee employee) {
		// 生成数据权限过滤条件
		DataScopeUtils.genSql(employee, "dsf", "company_m,office_m", "c3,o2");
		return super.findPage(page, employee);
	}

	/**
	 * 分页查询不进行权限过滤的员工数据
	 * @param page
	 * @param employee
	 * @return
	 */
	public Page<Employee> findAllPage(Page<Employee> page, Employee employee) {
		return super.findPage(page, employee);
	}

	/**
	 * 保存员工
	 */
	@Transactional(readOnly = false)
	public void save(Employee employee) {
		boolean isNewRecord = false;
		if (employee.getIsNewRecord()) {
			isNewRecord = true;
			if (Global.isUseCorpModel()){
				employee.setId(employee.getCorpCode() + "_" + employee.getEmpNo());
			}else{
				employee.setId(employee.getEmpNo());
			}
			if (get(employee) != null){
				throw getValidationIdExistsException();
			}
		}
		//保存员工信息
		super.save(employee);
		//保存员工岗位信息
		dao.deleteEmployeePost(employee);
		if (employee.getPostList() != null && employee.getPostList().size() > 0) {
			dao.insertEmployeePost(employee);
		}
		//如果是新增的数据
		if (isNewRecord) {
			//如果是登录用户，则需要保存数据到用户表
			if (employee.getIsLoginUser().equals(Global.YES)) {
				User loginUser = employee.getLoginInfo();
				loginUser.setIsNewRecord(true);
				loginUser.setEmail(employee.getEmail());
				loginUser.setMobile(employee.getMobile());
				loginUser.setPhone(employee.getOfficePhone());
				loginUser.setRefCode(employee.getEmpCode());
				loginUser.setRefName(employee.getEmpName());
				loginUser.setUserType(User.USER_TYPE_EMPLOYEE);//员工
				loginUser.setMgrType(User.MGR_TYPE_NOT_ADMIN);//非管理员
				userService.addUser(loginUser);
			}
		}else{
			User user = new User();
			user.setRefCode(employee.getId());
			user.setRefName(employee.getEmpName());
			user.setUserType(User.USER_TYPE_EMPLOYEE);
			userService.updateRefNameByRefCode(user);
		}
	}

	/**
	 * 删除员工
	 */
	@Transactional(readOnly = false)
	public void delete(Employee employee) {
		dao.delete(employee);
	}

	/**
	 * 删除员工（同delete）
	 * @param employee
	 * @return
	 */
	@Transactional(readOnly = false)
	public int deleteEmployee(Employee employee) {
		employee.setStatus(Employee.EMP_STATUS_DELETE);
		int count = dao.updateEmployeeStatus(employee);

		// 更新关联用户状态
		User user = new User();
		user.setUserType(User.USER_TYPE_EMPLOYEE);
		user.setRefCode(employee.getId());
		user.setStatus(User.STATUS_DELETE);
		userService.updateUserStatusByRefCode(user);
		
		return count;
	}

	/**
	 * 员工离职
	 * @param employee
	 * @return
	 */
	@Transactional(readOnly = false)
	public int leaveEmployee(Employee employee) {
		employee.setStatus(Employee.EMP_STATUS_LEAVE);
		int count = dao.updateEmployeeStatus(employee);

		// 更新关联用户状态
		User user = new User();
		user.setUserType(User.USER_TYPE_EMPLOYEE);
		user.setRefCode(employee.getId());
		user.setStatus(User.STATUS_DISABLE);
		userService.updateUserStatusByRefCode(user);
		
		return count;
	}

	/**
	 * 启用员工
	 * @param employee
	 * @return
	 */
	@Transactional(readOnly = false)
	public int activateEmployee(Employee employee) {
		employee.setStatus(Employee.EMP_STATUS_NORMAL);
		int count = dao.updateEmployeeStatus(employee);

		// 更新关联用户状态
		User user = new User();
		user.setUserType(User.USER_TYPE_EMPLOYEE);
		user.setRefCode(employee.getId());
		user.setStatus(User.STATUS_NORMAL);
		userService.updateUserStatusByRefCode(user);
		
		return count;
	}

	/**
	 * 根据工号获取员工信息
	 * @param employee
	 * @return
	 */
	public Employee getByEmpNo(String empNo) {
		Employee employee = new Employee();
		employee.setEmpNo(empNo);
		return dao.getByEmpNo(employee);
	}

	/** 
	 * 查询导出员工的列表
	 * @author jiangwenchao
	 * @param employee
	 */
	public List<Employee> findExportList(Employee employee) {
		List<Employee> list = findList(employee);
		List<Employee> newList = Lists.newArrayList();
		for (Employee e : list) {
			if (e != null && StringUtils.isNotBlank(e.getEmpCode())) {
				e.setPostList(postService.getPostByEmployee(e));
				newList.add(e);
			}
		}
		return newList;
	}

	@Transactional(readOnly = false)
	public void insertEmp(Employee employee) {
		dao.insert(employee);
	}

}